
//
//  ViewController2.m
//  20202notification
//
//  Created by Clement_Gu on 16/3/16.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()
@property (nonatomic,strong) UIView *sbView;
@property (nonatomic,strong) UISlider *sliderRed;
@property (nonatomic,strong) UISlider *sliderGreen;
@property (nonatomic,strong) UISlider *sliderBlue;
@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //背景色
    self.view.backgroundColor = [UIColor whiteColor];
    //创建导航条按键
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = barBtn;
    //创建调色板
    //创建view
    UIView *sbView = [[UIView alloc]initWithFrame:CGRectMake(60, 70, 200, 150)];
    self.sbView = sbView;
    [self.view addSubview:sbView];
    //创建slider
    UISlider *sliderRed = [[UISlider alloc]initWithFrame:CGRectMake(10, 250, 300, 30)];
    sliderRed.minimumValue = 0;
    sliderRed.maximumValue = 1;
    UISlider *sliderGreen = [[UISlider alloc]initWithFrame:CGRectMake(10, 300, 300, 30)];
    sliderGreen.minimumValue = 0;
    sliderGreen.maximumValue = 1;
    UISlider *sliderBlue = [[UISlider alloc]initWithFrame:CGRectMake(10, 350, 300, 30)];
    sliderBlue.minimumValue = 0;
    sliderBlue.maximumValue = 1;
    //添加属性
    self.sliderRed = sliderRed;
    self.sliderGreen = sliderGreen;
    self.sliderBlue = sliderBlue;
    //添加事件
    [sliderRed addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged];
    [sliderGreen addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged];
    [sliderBlue addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged];
    //添加到页面
    [self.view addSubview:sliderRed];
    [self.view addSubview:sliderGreen];
    [self.view addSubview:sliderBlue];
    //显示颜色
    [self updateData];
  
    //创建监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateData) name:UIApplicationWillEnterForegroundNotification object:nil];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
}
//移除监听
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)updateData
{
    //读取settings的数值作为slider的初始值
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.sliderRed.value = [user floatForKey:@"red"];
    self.sliderGreen.value = [user floatForKey:@"green"];
    self.sliderBlue.value = [user floatForKey:@"blue"];
    //显示初始颜色
//    self.sbView.backgroundColor = [UIColor colorWithRed:[user floatForKey:@"red"] green:[user floatForKey:@"green"] blue:[user floatForKey:@"blue"] alpha:1];
    [self backcolor];

}
//view的背景色
-(void)backcolor
{
    self.sbView.backgroundColor = [UIColor colorWithRed:self.sliderRed.value green:self.sliderGreen.value blue:self.sliderBlue.value alpha:1];
}

//改变红颜色
-(void)changeColor
{
    [self backcolor];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setFloat:self.sliderRed.value forKey:@"red"];
    [user setFloat:self.sliderGreen.value forKey:@"green"];
    [user setFloat:self.sliderBlue.value forKey:@"blue"];
    [user synchronize];
}

//返回前一页面
-(void)back
{
    [self.Delegate getColorred:self.sliderRed.value green:self.sliderGreen.value blue:self.sliderBlue.value];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
